package xyz.jcat.webmvc.validator;

import xyz.jcat.common.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

public class EnumStrValidator implements ConstraintValidator<EnumStr, String> {

    private List<String> enums;

    @Override
    public void initialize(EnumStr constraintAnnotation) {
        enums = Arrays.asList(constraintAnnotation.values());
    }

    @Override
    public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isBlank(str)) {
            return true;
        }
        return enums.contains(str);
    }

}
